<template>
  <el-dialog title="发送优惠券" :visible.sync="visible" :close-on-click-modal="false">
    <el-form ref="form" :model="form" :rules="rules">
      <el-form-item label="每人发送张数" :label-width="formLabelWidth" prop="getNumber">
        <el-input-number
            :min="0"
            :max="this.coupon.getMax"
            v-model="form.getNumber"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="优惠券名称" :label-width="formLabelWidth" prop="couponName">
        <el-input
            v-model="form.couponName"
            autocomplete="off"
            placeholder="请输入优惠券名称"
            disabled
        ></el-input>
      </el-form-item>
      <el-form-item label="发送类型" :label-width="formLabelWidth" prop="sendType">
        <fortress-radio-group
            v-model="form.sendType" :dictList="CouponSendTypeList">
        </fortress-radio-group>
      </el-form-item>
      <el-form-item label="指定用户" v-if="form.sendType==1" :label-width="formLabelWidth" prop="receiverUserIds">
        <v-auditer v-model="value"></v-auditer>
      </el-form-item>
      <el-form-item label="指定等级" v-if="form.sendType==2" :label-width="formLabelWidth" prop="receiverUserLevelIds">
        <el-select v-model="form.receiverUserLevelIds" multiple placeholder="请选择">
          <el-option
              v-for="item in limitLevelIdsList"
              :key="item.code"
              :label="item.name"
              :value="item.code">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false" size="medium" type="danger">取 消</el-button>
      <el-button type="primary" size="medium" @click="submitForm('form')">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import {salecouponSend} from "@/api/orders/salecoupon.js";
import {urlList, enumList} from "@/api/public.api.js";
import * as ftVal from "@/common/fortressValidator.js";

export default {
  data() {
    return {
      coupon: {},
      value: {
        dept: [],
        user: [],
      },
      visible: false,
      form: {
        couponId: "",
        couponName: "",
        getNumber: "",
        sendType: "",
        receiverUserIds: "",
        receiverUserLevelIds: [],
      },
      formLabelWidth: "200px",
      rules: {
        enough: [
          {required: true, message: "请输入消费满多少可用 0 不限制", trigger: "blur"},
          {validator: ftVal.validatePInteger, trigger: "blur"},
        ],
      },
      limitLevelIdsList: [],
      CouponSendTypeList: [],
    };
  },
  created() {
  },
  methods: {
    queryUrlList() {
      this.limitLevelIdsList = [];
      urlList("/member/memberLevel/list").then((resp) => {
        if (resp.code == 0) {
          if (resp.data) {
            resp.data.forEach((item) => {
              this.limitLevelIdsList.push({code: item.id, name: item.name});
            });
          }
        }
      });
    },
    init(coupon) {
      this.resetForm();
      this.queryUrlList();
      this.queryEnumList();
      this.form.couponId = coupon.id
      this.form.couponName = coupon.couponName
      this.coupon = coupon;
      this.visible = true;
    },
    queryEnumList() {
      this.CouponSendTypeList = [];
      enumList("CouponSendType", "orders").then((resp) => {
        if (resp.code == 0) {
          this.CouponSendTypeList = resp.data;
        }
      });
    },
    //提交表单数据
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let requestForm = {...this.form, receiverUserLevelIds: this.form.receiverUserLevelIds.join(",")}
          // requestForm.receiverUserIds = requestForm.receiverUserIds.join(",")
          salecouponSend(requestForm).then((resp) => {
            if (resp.code == 0) {
              this.$message({
                type: "success",
                message: "操作成功!",
              });
              this.$emit("refreshData");
              this.visible = false;
            } else {
              this.$msg({
                message: resp.msg,
                type: "error",
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm() {
      this.form = {
        couponId: "",
        couponName: "",
        getNumber: "",
        sendType: "",
        receiverUserIds: "",
        receiverUserLevelIds: [],
      };
      this.$nextTick(() => {
        if (this.$refs["form"]) {
          this.$refs["form"].resetFields();
        }
      });
    },
  },
};
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.el-scrollbar .el-scrollbar__view .el-select-dropdown__item {
  height: auto;
  max-height: 274px;
  padding: 7px;
  overflow: hidden;
  overflow-y: auto;
  font-weight: normal;
}
</style>
